import java.util.concurrent.ArrayBlockingQueue;

public class h_2_Cooker implements  Runnable {
    private ArrayBlockingQueue arrayBlockingQueue;
    private String name;
    public h_2_Cooker(ArrayBlockingQueue arrayBlockingQueue,String name){
        this.arrayBlockingQueue=arrayBlockingQueue;
        this.name=name;
    }
    @Override
    public void run() {
        Thread.currentThread().setName(name);
        while(true){
            while(arrayBlockingQueue.size()>=5){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if(arrayBlockingQueue.size()<5){
                boolean add = arrayBlockingQueue.add("汉堡");
                System.out.println(Thread.currentThread().getName()+"放了一个汉堡，现存"+arrayBlockingQueue.size()+"个");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
